package api.server;

import com.ycy.lucene.index.IndexReadDemo;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer6x;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

@Slf4j
public class IndexDemo2 {

  //源文件
  public static String IndexPath;


  static {
    // 获取用户目录
    String userDirectory = System.getProperty("user.home");
    // 打印用户目录
    log.info("用户目录: " + userDirectory);
    // 获取文件分隔符
    String fileSeparator = System.getProperty("file.separator");
    // 打印文件分隔符
    log.info("文件分隔符: " + fileSeparator);


    IndexPath = userDirectory + fileSeparator + "Desktop" + fileSeparator + "linshilucenedata" + fileSeparator + "ix2";
    log.info("数据源目录 -> {} 索引生成目录 -> {}", "pathname", IndexPath);
  }

  public static void main(String[] args) {
    try {
      Path path = Paths.get(IndexPath);
      FSDirectory directory = FSDirectory.open(path);

      DirectoryReader open = DirectoryReader.open(directory);
      IndexSearcher indexSearcher = new IndexSearcher(open);
      Query query = new MatchAllDocsQuery();

      // 执行搜索
      TopDocs results = indexSearcher.search(query, 10);

      for (ScoreDoc scoreDoc : results.scoreDocs) {
        Document doc = indexSearcher.doc(scoreDoc.doc);
        System.out.println("Document ID: " + scoreDoc.doc + " - " + doc.get("id"));
      }


    } catch (IOException e) {
      e.printStackTrace();
    }
  }


}
